package main

import (
	"fmt"
	"sync"
)

func main() {
	// 创建带缓冲的 channel，用于传递任务
	const taskCount = 100
	const workerCount = 10

	jobs := make(chan int, taskCount)
	var wg sync.WaitGroup

	// 启动 10 个消费者
	for w := 1; w <= workerCount; w++ {
		wg.Add(1)
		go func(workerID int) {
			defer wg.Done()
			for num := range jobs {
				fmt.Printf("Worker %d processed number: %d\n", workerID, num)
			}
		}(w)
	}

	// 生产者：发送 1 到 100 的数字到 channel 中
	for i := 1; i <= taskCount; i++ {
		jobs <- i
	}
	close(jobs) // 所有任务发送完成，关闭 channel

	// 等待所有消费者处理完毕
	wg.Wait()
	fmt.Println("All tasks have been processed.")
}
